/* 
*** Downstream effects by representation of women in the union (establishment data)
	// input: estlevel_dataset, estlevel_dataset_full, microregions_municipality_concordance, unionboards_SDSR_clean, shdaysleave, shdaysleavefem
	// output: TableA15 (sheet: firmenv, empl, absences)
*/ 

cap log close
cap log using "$logs/establevel_heterog1_log", replace


*********************************
** CHANGES IN FIRM ENVIRONMENT **
*********************************

* data toggles
local singest = 0 //0: no restriction; 1: must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: must employ both men and women at basline
local covered = 1 //0: no restriction; 1: must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: must be a signing establishment
local geoglevel = "microregion_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local balanced = 0 //0: no restriction; 1: restrict to the balanced panel

	* Load establishment sample (main spells)
	use "$files/estlevel_dataset.dta", clear

	* sample restrictions
	if `singest' {
		keep if bl_singest==1
	}
	if `bothFM' {
		keep if bl_bothFM==1
	}
	if `covered' {
		keep if (bl_covered==1)
	}
	if `signing' {
		keep if (bl_signing==1)
	}
	if `balanced' {
		egen xxx = max(exit), by(fakeid_estab)
		keep if xxx==0
		drop xxx
	}
	
	* time varying fixed effects
	//industry
	gen ind2d_num = floor(ind_mode/1e3)
	//geography
	gen state_mode = floor(municipality_mode/1e4)
	rename municipality_mode municipality
	merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
	drop if _merge==2
	drop _merge
	//fixed effects
	rename municipality municipality_mode
	rename microregion microregion_mode
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}

	* for clustering
	egen clustergrp = group(fakeid_estab)

	* event study var 
	gen post=(year>=2015) if year!=.
	gen treatpost=treat*post
	tab year, gen(yearj)
		
	forvalues i = 1/7 {
		gen interj`i'=treat*yearj`i'
	}

	lab var interj1 "2011"
	lab var interj2 "2012"
	lab var interj3 "2013"
	lab var interj4 "2014"
	lab var interj5 "2015"
	lab var interj6 "2016"
	lab var interj7 "2017"

	gen zero=0
	lab var zero "2014"
	replace interj4 = zero

	* Outcome variables
	gen sh_mgmt_fem_mgmt = mgmt_fem/mgmt
	label var sh_mgmt_fem_mgmt 		"Share of female managers (among management)"
	gen sh_mlext_mleav = mlext_mleav/mleav
	label var sh_mlext_mleav			"Share on extended maternity leave (among workers on maternity leave)"
	
	* Heterogeneity
	//share of women at baseline
	gen sharefemale = tot_fem/tot
	gen xxx = sharefemale if year==2014
	egen sharefemale_2014 = max(xxx), by(fakeid_estab)
	drop xxx
	sum sharefemale_2014 if year==2014, d 
	local median = r(p50)
	gen high_sharefemale_2014 = (sharefemale_2014>`median') if sharefemale_2014!=.
	//union board info
	// 1) Share of women in union board at baseline
	// 2) female Pres/VP at baseline
	preserve
		use "$files/unionboards_SDSR_clean.dta", clear
		keep union_id sh_female_bl female_president_bl female_vicepresident_bl union_type
		//tag unions with info on boards at baseline 
		gen has_bl = (sh_female_bl!=. & female_president_bl!=.)
		duplicates drop
		sum sh_female_bl, d 
		sum sh_female_bl if union_type=="Trabalhador", d 
		gen high_sh_female_bl = (sh_female_bl>1/3) if sh_female_bl!=.
		egen female_PorVP = rowmax( female_president_bl female_vicepresident_bl)
		tab high_sh_female_bl
		tab high_sh_female_bl if union_type=="Trabalhador"
		tab female_PorVP
		tab female_PorVP if union_type=="Trabalhador"
		tempfile baselines
		save `baselines' 
	restore	
	merge m:1 union_id  using `baselines' , keep(1 3)
	
	* SOME COLUMNS ON TABLE WILL BE RESTRICTED TO HAVING INFO ON BOARD AT BASELINE
	tab has_bl
	unique union_id
	unique union_id if has_bl!=1
	unique fakeid_estab
	unique fakeid_estab if has_bl!=1
	mdesc high_sh_female_bl female_PorVP high_sharefemale_2014

	* Regressions
	if `no2011' {
		drop if year==2011
		drop interj1
		local xl = 3.5
	}
	else {
		local xl = 4.5
	}
	
	* main results
	local i = 0
	foreach var of varlist sh_mgmt_fem_mgmt sh_mlext_mleav {
		local i = `i'+1 
		qui reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		su `var' if treat == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N_full)
		estimates store al`i'
	}		
	
	* interact with low female share (union boards)
	gen dummy = (high_sh_female_bl==0) if high_sh_female_bl!=.
	local i = 0
	foreach var of varlist sh_mgmt_fem_mgmt sh_mlext_mleav {
		local i = `i'+1 
		qui reghdfe `var' treatpost##dummy treat##dummy post##dummy, absorb(fakeid_estab *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store bl`i'
	}			
	drop dummy
		
	*** Export ***
	forvalues j = 1/2 {
			#d ;
			estout al`j' bl`j' using "$tables/T4_1het_firmenv`j'.txt", 
			style(tab) mlabels(none) label collabels(none)
			cells(b(star fmt(%9.3f)) se(par)) 
			stats(sumdum sumdump meandv obs, fmt(%9.3f %9.3f %9.2fc %9.0fc ) labels("sum" "p-value" "mean depvar" "N" )) 
			drop(o.*, relax) keep(treatpost 1.treatpost 1.treatpost#1.dummy, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
			#d cr
		}	
	
	local lab1 sh_mgmt_fem_mgmt
	local lab2 sh_mlext_mleav
	
	local r1 = 1
	local r2 = 12 	
	
	import delimited "$tables/T4_1het_firmenv1.txt", clear
	rename v1 `lab1'
	rename v2 main
	rename v3 low_femunion
			
	export excel using "$tables/TableA15.xlsx", sheet("firmenv") cell(A`r1') firstrow(var) sheetreplace

	
	forvalues p = 2/2 {
	
	import delimited "$tables/T4_1het_firmenv`p'.txt", clear
	label var v1 `lab`p''
	label var v2 " "
	label var v3 " "
			
	export excel using "$tables/TableA15.xlsx", sheet("firmenv") cell(A`r`p'') firstrow(varl) sheetmodify
	}
	


***********************************************************************
** IMPACT OF CUT REFORM ON ESTABLISHMENT LEVEL OUTCOMES (all spells) **
***********************************************************************

* data toggles
local singest = 0 //0: no restriction; 1: must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: must employ both men and women at basline
local covered = 1 //0: no restriction; 1: must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: must be a signing establishment
local geoglevel = "microregion_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local balanced = 0 //0: no restriction; 1: restrict to the balanced panel

	* Load establishment sample (all spells)
	use "$files/estlevel_dataset_full.dta", clear

	* sample restrictions
	if `singest' {
		keep if bl_singest==1
	}
	if `bothFM' {
		keep if bl_bothFM==1
	}
	if `covered' {
		keep if (bl_covered==1)
	}
	if `signing' {
		keep if (bl_signing==1)
	}
	if `balanced' {
		egen xxx = max(exit), by(fakeid_estab)
		keep if xxx==0
		drop xxx
	}
	
	* time varying fixed effects
	//industry
	gen ind2d_num = floor(ind_mode/1e3)
	//geography
	gen state_mode = floor(municipality_mode/1e4)
	rename municipality_mode municipality
	merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
	drop if _merge==2
	drop _merge
	//fixed effects
	rename municipality municipality_mode
	rename microregion microregion_mode
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}

	* for clustering
	egen clustergrp = group(fakeid_estab)

	* event study var 
	gen post=(year>=2015) if year!=.
	gen treatpost=treat*post
	tab year, gen(yearj)
		
	forvalues i = 1/7 {
		gen interj`i'=treat*yearj`i'
	}

	lab var interj1 "2011"
	lab var interj2 "2012"
	lab var interj3 "2013"
	lab var interj4 "2014"
	lab var interj5 "2015"
	lab var interj6 "2016"
	lab var interj7 "2017"

	gen zero=0
	lab var zero "2014"
	replace interj4 = zero

	* Outcomes
	gen y_sh_tot_fem_tot = tot_fem/tot
	label var y_sh_tot_fem_tot 		"Share female (all workers)"
	gen y_sh_probw1_fem = probw1_fem/probw1
	label var y_sh_probw1_fem 		"Share female (probationary workers)"
	
	* Heterogeneity
	//share of women at baseline
	gen sharefemale = tot_fem/tot
	gen xxx = sharefemale if year==2014
	egen sharefemale_2014 = max(xxx), by(fakeid_estab)
	drop xxx
	sum sharefemale_2014 if year==2014, d 
	local median = r(p50)
	gen high_sharefemale_2014 = (sharefemale_2014>`median') if sharefemale_2014!=.
	//union board info
	// 1) Share of women in union board at baseline
	// 2) female Pres/VP at baseline
	preserve
		use "$files/unionboards_SDSR_clean.dta", clear
		keep union_id sh_female_bl female_president_bl female_vicepresident_bl union_type
		//tag unions with info on boards at baseline 
		gen has_bl = (sh_female_bl!=. & female_president_bl!=.)
		duplicates drop
		sum sh_female_bl, d 
		sum sh_female_bl if union_type=="Trabalhador", d 
		gen high_sh_female_bl = (sh_female_bl>1/3) if sh_female_bl!=.
		egen female_PorVP = rowmax( female_president_bl female_vicepresident_bl)
		tab high_sh_female_bl
		tab high_sh_female_bl if union_type=="Trabalhador"
		tab female_PorVP
		tab female_PorVP if union_type=="Trabalhador"
		tempfile baselines
		save `baselines' 
	restore	
	merge m:1 union_id  using `baselines' , keep(1 3)
	
	* SOME COLUMNS ON TABLE WILL BE RESTRICTED TO HAVING INFO ON BOARD AT BASELINE
	tab has_bl
	unique union_id
	unique union_id if has_bl!=1 
	unique fakeid_estab
	unique fakeid_estab if has_bl!=1 
	mdesc high_sh_female_bl female_PorVP high_sharefemale_2014
	
	* Regressions
	if `no2011' {
		drop if year==2011
		drop interj1
		local xl = 3.5
	}
	else {
		local xl = 4.5
	}
	
	* main results
	local i = 0
	foreach var of varlist y_sh_tot_fem_tot y_sh_probw1_fem {
		local i = `i'+1 
		qui reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		su `var' if treat == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store al`i'
	}	
	
	* interact with low female share (union boards)
	gen dummy = (high_sh_female_bl==0) if high_sh_female_bl!=.
	local i = 0
	foreach var of varlist y_sh_tot_fem_tot y_sh_probw1_fem {
		local i = `i'+1 
		qui reghdfe `var' treatpost##dummy treat##dummy post##dummy, absorb(fakeid_estab *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store bl`i'
	}	
	drop dummy
		
	*** Export ***
	forvalues j = 1/2 {
			#d ;
			estout al`j' bl`j' using "$tables/T4_1het_empl`j'.txt", 
			style(tab) mlabels(none) label collabels(none)
			cells(b(star fmt(%9.3f)) se(par)) 
			stats(sumdum sumdump meandv obs, fmt(%9.3f %9.3f %9.2fc %9.0fc ) labels("sum" "p-value" "mean depvar" "N" )) 
			drop(o.*, relax) keep(treatpost 1.treatpost 1.treatpost#1.dummy, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
			#d cr
		}	
		
	local lab1 y_sh_tot_fem_tot
	local lab2 y_sh_probw1_fem
	
	local r1 = 1
	local r2 = 12 
	
	import delimited "$tables/T4_1het_empl1.txt", clear
	rename v1 `lab1'
	rename v2 main
	rename v3 low_femunion
			
	export excel using "$tables/TableA15.xlsx", sheet("empl") cell(A`r1') firstrow(var) sheetreplace

	
	forvalues p = 2/2 {
	
	import delimited "$tables/T4_1het_empl`p'.txt", clear
	label var v1 `lab`p''
	label var v2 " "
	label var v3 " "
			
	export excel using "$tables/TableA15.xlsx", sheet("empl") cell(A`r`p'') firstrow(varl) sheetmodify
	}
	

*********************************************************************
** IMPACT OF CUT REFORM ON ESTABLISHMENT LEVEL OUTCOMES (absences) **
*********************************************************************

* data toggles
local singest = 0 //0: no restriction; 1: must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: must employ both men and women at basline
local covered = 1 //0: no restriction; 1: must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: must be a signing establishment
local geoglevel = "microregion_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local balanced = 0 //0: no restriction; 1: restrict to the balanced panel

	* Load establishment sample (main spells)
	use "$files/estlevel_dataset.dta", clear
	merge 1:1 fakeid_estab year using "$files/shdaysleave.dta"
	drop if _merge==2
	drop _merge
	merge 1:1 fakeid_estab year using "$files/shdaysleavefem.dta"
	drop if _merge==2
	drop _merge

	* sample restrictions
	if `singest' {
		keep if bl_singest==1
	}
	if `bothFM' {
		keep if bl_bothFM==1
	}
	if `covered' {
		keep if (bl_covered==1)
	}
	if `signing' {
		keep if (bl_signing==1)
	}
	if `balanced' {
		egen xxx = max(exit), by(fakeid_estab)
		keep if xxx==0
		drop xxx
	}
	
	* time varying fixed effects
	//industry
	gen ind2d_num = floor(ind_mode/1e3)
	//geography
	gen state_mode = floor(municipality_mode/1e4)
	rename municipality_mode municipality
	merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
	drop if _merge==2
	drop _merge
	//fixed effects
	rename municipality municipality_mode
	rename microregion microregion_mode
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}

	* for clustering
	egen clustergrp = group(fakeid_estab)

	* event study var 
	gen post=(year>=2015) if year!=.
	gen treatpost=treat*post
	tab year, gen(yearj)
		
	forvalues i = 1/7 {
		gen interj`i'=treat*yearj`i'
	}

	lab var interj1 "2011"
	lab var interj2 "2012"
	lab var interj3 "2013"
	lab var interj4 "2014"
	lab var interj5 "2015"
	lab var interj6 "2016"
	lab var interj7 "2017"

	gen zero=0
	lab var zero "2014"
	replace interj4 = zero

	* Outcomes
	gen y_shdaysleave_alt = (shdaysleave-shdaysleave_50-shdaysleave_60)*100
	label var y_shdaysleave_alt 	"Workdays lost to leaves (%)"
	
	* Heterogeneity
	//share of women at baseline
	gen sharefemale = tot_fem/tot
	gen xxx = sharefemale if year==2014
	egen sharefemale_2014 = max(xxx), by(fakeid_estab)
	drop xxx
	sum sharefemale_2014 if year==2014, d 
	local median = r(p50)
	gen high_sharefemale_2014 = (sharefemale_2014>`median') if sharefemale_2014!=.
	//union board info
	// 1) Share of women in union board at baseline
	// 2) female Pres/VP at baseline
	preserve
		use "$files/unionboards_SDSR_clean.dta", clear
		keep union_id sh_female_bl female_president_bl female_vicepresident_bl union_type
		//tag unions with info on boards at baseline 
		gen has_bl = (sh_female_bl!=. & female_president_bl!=.)
		duplicates drop
		sum sh_female_bl, d 
		sum sh_female_bl if union_type=="Trabalhador", d 
		gen high_sh_female_bl = (sh_female_bl>1/3) if sh_female_bl!=.
		egen female_PorVP = rowmax( female_president_bl female_vicepresident_bl)
		tab high_sh_female_bl
		tab high_sh_female_bl if union_type=="Trabalhador"
		tab female_PorVP
		tab female_PorVP if union_type=="Trabalhador"
		tempfile baselines
		save `baselines' 
	restore	
	merge m:1 union_id  using `baselines' , keep(1 3)
	
	* SOME COLUMNS ON TABLE WILL BE RESTRICTED TO HAVING INFO ON BOARD AT BASELINE
	tab has_bl
	unique union_id
	unique union_id if has_bl!=1 
	unique fakeid_estab
	unique fakeid_estab if has_bl!=1 
	mdesc high_sh_female_bl female_PorVP high_sharefemale_2014
	
	* Regressions
	if `no2011' {
		drop if year==2011
		drop interj1
		local xl = 3.5
	}
	else {
		local xl = 4.5
	}
	
	* main results
	local i = 0
	foreach var of varlist y_shdaysleave_alt {
		local i = `i'+1 
		qui reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		su `var' if treat == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store al`i'
	}	
	
	* interact with low female share (union boards)
	gen dummy = (high_sh_female_bl==0) if high_sh_female_bl!=.
	local i = 0
	foreach var of varlist y_shdaysleave_alt {
		local i = `i'+1 
		qui reghdfe `var' treatpost##dummy treat##dummy post##dummy, absorb(fakeid_estab *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store bl`i'
	}	
	drop dummy
		
	*** Export ***
	forvalues j = 1/1 {
			#d ;
			estout al`j' bl`j' using "$tables/T4_1het_workd`j'.txt", 
			style(tab) mlabels(none) label collabels(none)
			cells(b(star fmt(%9.3f)) se(par)) 
			stats(sumdum sumdump meandv obs, fmt(%9.3f %9.3f %9.2fc %9.0fc ) labels("sum" "p-value" "mean depvar" "N" )) 
			drop(o.*, relax) keep(treatpost 1.treatpost 1.treatpost#1.dummy, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
			#d cr
		}	
		
	local lab1 y_shdaysleave_alt 
	
	local r1 = 1
	
	import delimited "$tables/T4_1het_workd1.txt", clear
	rename v1 `lab1'
	rename v2 main
	rename v3 low_femunion
			
	export excel using "$tables/TableA15.xlsx", sheet("absences") cell(A`r1') firstrow(var) sheetreplace


cap log close
